﻿using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.SpecialUtils.Html;
using System;

namespace SinaImgUpload.SainTool.Configuration
{
    internal class ConfigurationHelper
    {
        /// <summary>
        /// 初始化文件
        /// </summary>
        public void InitFile()
        {
            string Content = "";
            Content += CreateXml("compressimg", "false");
            Content += CreateXml("watermark", "false");
            FileHelper.WriteText(ConfigurationValue.ConfigurationPath1, Content);
        }

        /// <summary>
        /// 初始化信息，包括文件
        /// </summary>
        public void InitInfo()
        {
            if (!FileHelper.FileExits(ConfigurationValue.ConfigurationPath1))
                InitFile();

            ConfigurationValue.CompressImg = GetWithBool("//compressimg");
            ConfigurationValue.Watermark = GetWithBool("//watermark");
            Console.WriteLine(ConfigurationValue.CompressImg);
            Console.WriteLine(ConfigurationValue.Watermark);
        }

        /// <summary>
        /// 获取布尔值
        /// </summary>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        private bool GetWithBool(string Xpath)
        {
            string Content = FileHelper.ReadAllText(ConfigurationValue.ConfigurationPath1);
            string ModeText = HtmlHelper.GetFirstText(Content, Xpath);
            if (ModeText == "true")
                return true;
            return false;
        }

        private string CreateXml(string XmlName, string XmlValue)
        {
            string Modetext = "<{0}>{1}</{0}>\r\n";
            return string.Format(Modetext, XmlName, XmlValue);
        }
    }
}